package com.yuan.pay_mall.controller;

import com.yuan.pay_mall.form.ShippingForm;
import com.yuan.pay_mall.pojo.User;
import com.yuan.pay_mall.service.impl.ShippingServiceImpl;
import com.yuan.pay_mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import static com.yuan.pay_mall.consts.MallConst.CURRENT_USER;

@RestController
public class ShippingController {
    @Autowired
    private ShippingServiceImpl shippingService;
    @GetMapping("/shippings")
    public ResponseVo add(@Valid @RequestBody ShippingForm form, HttpSession session){
        User user = (User)session.getAttribute(CURRENT_USER);
        return shippingService.add(user.getId(),form);
    }
    @DeleteMapping("/shippings/{shippingId}")
    public ResponseVo delete(@Valid @RequestBody Integer shippingId,
                             HttpSession session){
        User user = (User)session.getAttribute(CURRENT_USER);
        return shippingService.delete(user.getId(),shippingId);
    }
    @PutMapping("/shippings/{shippingId}")
    public ResponseVo update(@Valid @RequestBody Integer shippingId,
                             @PathVariable ShippingForm form,
                             HttpSession session) {
        User user = (User)session.getAttribute(CURRENT_USER);
       return shippingService.upadate(user.getId(),shippingId,form);
    }
    @GetMapping("/shippings")
    public ResponseVo list(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                           HttpSession session){
        User user = (User)session.getAttribute(CURRENT_USER);
        return shippingService.list(user.getId(), pageNum, pageSize);
    }

}
